package com.kangfu.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.kangfu.api.service.IFeedbackService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.db.domain.entity.Feedback;
import com.kangfu.db.mapper.FeedbackMapper;
import com.kangfu.db.model.request.FeedbackRequest;
import com.kangfu.db.model.vo.FeedbackVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FeedbackServiceImpl implements IFeedbackService {

    @Autowired
    public FeedbackMapper feedbackMapper;

    @Override
    public int insertFeedback(int userId, FeedbackRequest feedbackRequest) {
        if (StrUtil.isBlank(feedbackRequest.getContent())) {
            throw new BusinessException("反馈内容不能为空");
        }
        Feedback feedback = new Feedback();
        feedback.setContent(feedbackRequest.getContent());
        feedback.setUserId(userId);
        feedback.setCreateTime(LocalDateTime.now());
        return feedbackMapper.insertFeedback(feedback);
    }

    @Override
    public List<FeedbackVo> selectFeedbackListByUserId(int userId, int page) {
        if (page <= 1) {
            page = 1;
        }
        int pageSize = 5;
        int offset = (page - 1)* pageSize;
        return feedbackMapper.selectFeedbackListByUserId(userId, offset, pageSize);
    }

}
